#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

// 测试HTTP请求用的URL
// #define URL "http://162.14.107.118:8086/PC/modifyFoodPrice/0/6"// 测试HTTP请求用的URL
// 设置wifi接入信息(请根据您的WiFi信息进行修改)
const char* ssid = "lin";
const char* password = "heibaizi2000+";


int counter=0; // 计数器
 
void setup() {
// 初始化串口
  Serial.begin(115200);
    //设置ESP8266工作模式为无线终端模式
  WiFi.mode(WIFI_STA);
 
  //开始连接wifi
  WiFi.begin(ssid, password);
 
  //等待WiFi连接,连接成功打印IP
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("WiFi Connected!");
}
 
void loop() {
  // 每loop循环一次，计数器变量加1
    delay(1000);
  counter = counter+1;
  // 输出变量
  String txt=Serial.readString();
  if(txt.length()>0){
      
      Serial.println(txt);
      if(!txt.compareTo("hello"))
        esp8266Http("15");
      else
        esp8266Http("1");
      delay(1000);
  }
  txt="";
  delay(1000);
}

// 发送HTTP请求并且将服务器响应通过串口输出
void esp8266Http(String price){
  String  URL= "http://162.14.107.118:8086/PC/modifyFoodPrice/0/";
  URL=URL+price;
  price="";
  WiFiClient client; 
  //创建 HTTPClient 对象
  HTTPClient httpClient;
 
  //配置请求地址。此处也可以不使用端口号和PATH而单纯的
  httpClient.begin(client,URL); 
  Serial.print("URL: "); Serial.println(URL);
 
  //启动连接并发送HTTP请求
  int httpCode = httpClient.GET();
  Serial.print("Send GET request to URL: ");
  Serial.println(URL);
  
  //如果服务器响应OK则从服务器获取响应体信息并通过串口输出
  //如果服务器不响应OK则将服务器响应状态码通过串口输出
  if (httpCode == HTTP_CODE_OK) {
    String responsePayload = httpClient.getString();
    Serial.println("Server Response Payload: ");
    Serial.println(responsePayload);
  } else {
    Serial.println("Server Respose Code：");
    Serial.println(httpCode);
  }
 
  //关闭ESP8266与服务器连接
  httpClient.end();
}